import {AfterViewChecked, Component, OnInit, ViewChild} from '@angular/core';
import {Router} from "@angular/router";
import {fadeInAnimation} from "../../routing/route.animation";
import {NgForm} from "@angular/forms";
import {validateFormControls} from "../../shared/util/validate-utils";
import {LoginService} from "../../shared/service/login.service";
import {CurrUserService} from "../../shared/service/curr-user.service";
import {CurrUserModel} from "../../shared/models/curr-user.model";
import {CacheService} from "../../shared/service/cache.service";
import {myValidNull} from "../../shared/util/string-utils";
import {MdDialogService} from "../../shared/service/mdDialog.service";

@Component({
  selector: 'ms-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss'],
  providers:[LoginService],
  host: {
    '[@fadeInAnimation]': 'true'
  },
  animations: [ fadeInAnimation ]
})
export class LoginComponent implements OnInit {

  user:any={};
  currUser:CurrUserModel=  new CurrUserModel(null,null,null,null);
  constructor(
    private router: Router,
    private loginService:LoginService,
    private cacheService:CacheService,
    private currUserService:CurrUserService,
    private mdDialogService:MdDialogService,
  ) { }

  ngOnInit() {
    this.logout();
    this.currUserService.currUserChange.subscribe((currUserModel) => {
      this.currUser = currUserModel;
    });
  }

  login(e){
    if(this.validator()){
      this.loginService.login(this.user).then((user)=>{
        // console.log(user);
        //登录成功
        this.cacheService.setCurrUserInfo(user);
        let currUser = new CurrUserModel(user.id,user.realName,user.groupId,user.roleName);
        this.currUserService.setCurrUser(currUser);
        this.router.navigate(['/auth-guard']);
      },(error)=>{//后台验证错误再根据具体错误指定 control dialog
        console.log("登录户出错",error);
        let jsonResult = JSON.parse(error._body)
        if(jsonResult.errCode=="SERVICE/ERR_PASSWORD_INCORRECT"||jsonResult.errCode=="SERVICE/ERR_SYS_USER_NOT_EXIST"){
          this.plainTip("用户名或密码不正确！");
        }else {
          this.plainTip(jsonResult.msg);

        }
      })
    }
  }

  /**
   * 验证数据合法性，返回true 则说明可以进行保存
   *
   * @returns {boolean}
   */
  validator():boolean{
    let result:boolean = true;
    if(myValidNull(this.user.loginName)){
      result = false;
      this.plainTip("账号不能为空！");
    }
    if(result&&this.user.loginName.length >20){
      result = false;
      this.plainTip("账号不能超过20个字！");
    }
    if(result&&myValidNull(this.user.password)){
      result = false;
      this.plainTip("密码不能为空！");
    }
    if(result&&this.user.password.length >30){
      result = false;
      this.plainTip("密码不能超过20个字！");
    }
    return result;
  }

  plainTip(text){
    this.mdDialogService.plainTip(text).subscribe(result => {
      // console.log(result);
      // if(!myValidNull(result)&&result.status){//成功
      // }
    });
  }



  logout(){
    this.loginService.logout().then((result)=>{
      this.currUserService.clearCurrUser()
      this.cacheService.clear()
    })
  }


}
